package com.linchong.lock.service;


import com.linchong.lock.dao.OrderItemMapper;
import com.linchong.lock.dao.OrderMapper;
import com.linchong.lock.dao.ProductMapper;
import com.linchong.lock.model.Order;
import com.linchong.lock.model.OrderItem;
import com.linchong.lock.model.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Slf4j
public class OrderService {

	@Resource
	private OrderMapper orderMapper;
	@Resource
	private OrderItemMapper orderItemMapper;
	@Resource
	private ProductMapper productMapper;
	//购买商品id
	private int purchaseProductId = 100100;
	//购买商品数量
	private int purchaseProductNum = 1;

	@Autowired
	private PlatformTransactionManager platformTransactionManager;

	@Autowired
	private TransactionDefinition transactionDefinition;

	private Lock lock = new ReentrantLock();

	//@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public Integer createOrder() throws Exception {

		Product product;
		lock.lock();
		try {
			TransactionStatus transaction1 = platformTransactionManager.getTransaction(transactionDefinition);
			product = productMapper.selectByPrimaryKey(purchaseProductId);
			if (product == null) {
				platformTransactionManager.rollback(transaction1);
				throw new Exception("购买商品：" + purchaseProductId + "不存在");
			}
			//商品当前库存
			Integer currentCount = product.getCount();
			System.out.println(Thread.currentThread().getName() + "库存数：" + currentCount);
			//校验库存
			if (purchaseProductNum > currentCount) {
				platformTransactionManager.rollback(transaction1);
				throw new Exception("商品" + purchaseProductId + "仅剩" + currentCount + "件，无法购买");
			}
			productMapper.updateProductCount(purchaseProductNum, "xxx", new Date(), product.getId());
			platformTransactionManager.commit(transaction1);

		}finally {
			lock.unlock();
		}


		TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
		Order order = new Order();
		order.setOrderAmount(product.getPrice().multiply(new BigDecimal(purchaseProductNum)));
		//待处理
		handleOrder(order);

		handleOrderItem(product, order);

		platformTransactionManager.commit(transaction);
		return order.getId();
	}

	private void handleOrderItem(Product product, Order order) {
		OrderItem orderItem = new OrderItem();
		orderItem.setOrderId(order.getId());
		orderItem.setProductId(product.getId());
		orderItem.setPurchasePrice(product.getPrice());
		orderItem.setPurchaseNum(purchaseProductNum);
		orderItem.setCreateUser("xxx");
		orderItem.setCreateTime(new Date());
		orderItem.setUpdateTime(new Date());
		orderItem.setUpdateUser("xxx");
		orderItemMapper.insertSelective(orderItem);
	}

	private void handleOrder(Order order) {
		order.setOrderStatus(1);
		order.setReceiverName("xxx");
		order.setReceiverMobile("13311112222");
		order.setCreateTime(new Date());
		order.setCreateUser("xxx");
		order.setUpdateTime(new Date());
		order.setUpdateUser("xxx");
		orderMapper.insertSelective(order);
	}

}
